<template>
  <div>
    <!-- 客户池——新增、修改 -->
    <el-dialog
      v-model="openDetail"
      :close-on-click-modal="false"
      :title="detailTitle"
      width="500px"
    >
      <el-form
        ref="editForm"
        inline
        :model="detailForm"
        :rules="rules"
        label-width="85px"
      >
        <el-form-item label="客户简称" prop="customerSimpleName">
          <el-input v-model="detailForm.customerSimpleName" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="客户全称" prop="customerFullName">
          <el-input v-model="detailForm.customerFullName" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="detailForm.type" @change="fdsf">
            <el-option
              v-for="dict in dicts[DictDirection.客户类型]"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictSort"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="detailForm.contacts" style="width: 240px;"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="detailForm.telephone" style="width: 240px;"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="detailForm.address" style="width: 300px;"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitDetailForm">确定</el-button>
          <el-button @click="cancelDetailForm">取消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 客户池-分配 -->
    <el-dialog
      v-model="openDistribute"
      :close-on-click-modal="false"
      :title="distributeTitle"
      width="500px"
    >
      <el-form
        ref="distEditForm"
        inline
        :model="distributeForm"
        label-width="85px"
      >
        <el-form-item label="跟进人" prop="followUpPerson" :rules="[{ required: true, message: '请输入跟进人', trigger: 'blur' }]">
          <el-select v-model="distributeForm.followUpPerson">
            <el-option
              v-for="dict in dicts[DictDirection.跟进人]"
              :key="dict.deptId"
              :label="dict.nickName"
              :value="dict.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitDistributeForm">确定</el-button>
          <el-button @click="openDistribute = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineProps, getCurrentInstance, defineEmits, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import useDicts, { DictDirection } from '@/store/modules/dicts'

import {
  getCustomerData,
  setCustomerAdd,
  setCustomerDel,
  setCustomerUpdate,
  setCustomerFollow,
  getFollowPool,
  setStatus,
  getFollowDetail,
  addFollowDetail,
  editFollowDetail,
  delFollowDetail,
  listUserTab,
  setDistribute,
} from '@/api/customerAnalysis'

const dicts = useDicts()
dicts.setDicts(DictDirection.跟进人)
dicts.setDicts(DictDirection.客户类型)
const props = defineProps({
  userInfoList: {
    type: String,
  },
})
const // 新增表单状态
  openDetail = ref(false),
  // 跟进人状态
  openDistribute = ref(false),
  // 新增\修改表单标题
  detailTitle = ref(null),
  // 分配跟进人
  distributeTitle = ref(null),
  // 客户池新增修改表单
  detailForm = ref({
    id: undefined,
    customerSimpleName: undefined,
    customerFullName: undefined,
    type: undefined,
    contacts: undefined,
    telephone: undefined,
    address: undefined,
    status: undefined,
  }),
  // 客户池分配修改表单
  distributeForm = ref({
    id: undefined,
    ids: undefined,
    deptId: undefined,
    followUpPerson: undefined,
  }),
  // 查询条件
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    customerSimpleName: undefined,
    customerFullName: undefined,
    followUpPerson: undefined,
    type: undefined,
  }),
  // 客户池新增表单效验规则
  rules = ref({
    customerSimpleName: [{ required: true, message: '请输入客户简称', trigger: 'blur' }],
    customerFullName: [{ required: true, message: '请输入客户全称', trigger: 'blur' }],
    type: [{ required: true, message: '请输入类型', trigger: 'blur' }],
    contacts: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
    address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
    telephone: [
      { required: true, message: '手机号码不能为空', trigger: 'blur' },
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: '请输入正确的手机号码',
        trigger: 'blur',
      },
    ],
  })
const fdsf = (val) => {
  console.log(val)
}
// 重置新增，删除表单
const reset = () => {
    detailForm.value = {
      id: undefined,
      customerSimpleName: undefined,
      customerFullName: undefined,
      type: undefined,
      contacts: undefined,
      telephone: undefined,
      address: undefined,
      status: undefined,
    }
    $resetForm(editForm.value)
  },
  // 新增、修改表单
  editForm = ref(null),
  // 跟进人表单
  distEditForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()
const emit = defineEmits(['changeList'])
// 客户池新增/修改
const submitDetailForm = () => {
  $validateForm(editForm.value, () => {
    if (detailForm.value.id == undefined) {
      detailForm.value.status = 0
      setCustomerAdd(detailForm.value).then((res) => {
        if (res.code == 200) {
          ElMessage.success('新增成功')
          openDetail.value = false
          emit('changeList')
          reset()
        }
      })
    } else {
      const { type } = detailForm.value
      if (typeof type == 'string') {
        detailForm.value.type = type == '同行' ? 1 : type == '直客' ? 2 : ''
      }
      setCustomerUpdate(detailForm.value).then((res) => {
        if (res.code == 200) {
          ElMessage.success('修改成功')
          openDetail.value = false
          emit('changeList')
          reset()
        }
      })
    }
  })
}

// 取消新增/修改提交
const cancelDetailForm = () => {
  reset()
  openDetail.value = false
}

// 提交分配的跟进人
const submitDistributeForm = () => {
  $validateForm(distEditForm.value, () => {
    const data = { status: undefined, deptId: undefined, followUpPerson: undefined, ids: undefined }
    const { followUpPerson, ids } = distributeForm.value
    data.status = 1
    data.deptId = props.userInfoList
    if (distributeForm.value.id) {
      const { proxy } = getCurrentInstance() as any
      proxy.$parent.handleFollow(distributeForm.value)
    } else {
      setDistribute(data).then((res) => {
        if (res.code == 200) {
          ElMessage.success('分配成功')
          openDistribute.value = false
          emit('changeList')
        }
      })
    }
  })
}
// };
// 列表
const getList = () => {
  let localRes
  localRes = getCustomerData(queryParams.value).then((res) => res)
  // if (localRes.code == 200) {
  // 	detailForm.value = localRes.rows;
  // }
}
// 列表
defineExpose({
  openDetail,
  detailTitle,
  detailForm,
  openDistribute,
  distributeTitle,
  distributeForm,
  getList,
})
</script>
